package com.ruoyi.web.controller.lottery;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lottery.domain.LotTask;
import com.ruoyi.lottery.service.ILotTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 抽奖任务Controller
 *
 * @author shier
 * @date 2023-01-27
 */
@RestController
@RequestMapping("/lottery/task")
public class LotTaskController extends BaseController {

    @Autowired
    private ILotTaskService lotTaskService;

    /** 查询当前登录用户的待做任务 */
    @GetMapping("/queryWaitTask")
    public AjaxResult waitTask () {
        // TODO: 判断是否登录, 只有登陆了才能查询;否则排除异常

        // 基于 当前登录用户 查询 待做任务
        //lotTaskService.waitTaskByLoginUser(LotUser loginUser);
        List<LotTask> waitTasks = lotTaskService.waitTasksByLoginUser();


        return AjaxResult.success(waitTasks);
    }

    @PostMapping("taskFulfill")
    public AjaxResult taskFulfill(Long waitTaskId) {
        // TODO: 判断是否登录, 只有登陆了才能查询;否则排除异常

        // 完成任务
        String msg = lotTaskService.taskFulfill(waitTaskId);

        return AjaxResult.success(msg);
    }







    /**
     * 查询抽奖任务列表
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(LotTask lotTask) {
        startPage();
        List<LotTask> list = lotTaskService.selectLotTaskList(lotTask);
        return getDataTable(list);
    }

    /**
     * 导出抽奖任务列表
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:export')")
    @Log(title = "抽奖任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LotTask lotTask) {
        List<LotTask> list = lotTaskService.selectLotTaskList(lotTask);
        ExcelUtil<LotTask> util = new ExcelUtil<LotTask>(LotTask.class);
        util.exportExcel(response, list, "抽奖任务数据");
    }

    /**
     * 获取抽奖任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(lotTaskService.getById(id));
    }

    /**
     * 新增抽奖任务
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:add')")
    @Log(title = "抽奖任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LotTask lotTask) {
        return toAjax(lotTaskService.save(lotTask));
    }

    /**
     * 修改抽奖任务
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:edit')")
    @Log(title = "抽奖任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LotTask lotTask) {
        return toAjax(lotTaskService.updateById(lotTask));
    }

    /**
     * 删除抽奖任务
     */
    @PreAuthorize("@ss.hasPermi('lottery:task:remove')")
    @Log(title = "抽奖任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(lotTaskService.removeBatchByIds(Arrays.asList(ids)));
    }
}
